package top.z.orm.handle.osql.core;

import top.z.orm.core.osql.BeeSql;
import top.z.orm.core.osql.Condition;
import top.z.orm.core.osql.MoreObjToSQL;
import top.z.orm.core.osql.MoreTable;
import top.z.orm.core.osql.exception.BeeIllegalParameterException;

import java.util.Collections;
import java.util.List;

public class MoreObjSQL implements MoreTable {

    private BeeSql       beeSql;
    private MoreObjToSQL moreObjToSQL;

    public MoreObjSQL() {
    }

    public BeeSql getBeeSql() {
        if (this.beeSql == null) {
            beeSql = BeeFactory.getHoneyFactory().getBeeSql();
        }
        return beeSql;
    }

    public MoreObjSQL setBeeSql(BeeSql beeSql) {
        this.beeSql = beeSql;
        return this;
    }

    public MoreObjToSQL getMoreObjToSQL() {
        if (this.moreObjToSQL == null) {
            moreObjToSQL = BeeFactory.getHoneyFactory().getMoreObjToSQL();
        }
        return moreObjToSQL;
    }

    public MoreObjSQL setMoreObjToSQL(MoreObjToSQL moreObjToSQL) {
        this.moreObjToSQL = moreObjToSQL;
        return this;
    }

    @Override
    public <T> List<T> select(T entity) {
        if (entity == null) {
            return Collections.emptyList();
        }
        String sql = this.getMoreObjToSQL().toSelectSQL(entity);
        Logger.logSQL("select sql: ", sql);
        return getBeeSql().moreTableSelect(sql, entity);
    }

    @Override
    public <T> List<T> select(T entity, int start, int size) {
        if (entity == null) {
            return null;
        }
        if (size <= 0) {
            throw new BeeIllegalParameterException("Parameter 'size' need great than 0!");
        }
        if (start < 0) {
            throw new BeeIllegalParameterException("Parameter 'start' need great equal 0!");
        }
        String sql = this.getMoreObjToSQL().toSelectSQL(entity, start, size);
        Logger.logSQL("select sql: ", sql);
        return getBeeSql().moreTableSelect(sql, entity);
    }

    @Override
    public <T> List<T> select(T entity, Condition condition) {
        if (entity == null) {
            return null;
        }
        String sql = this.getMoreObjToSQL().toSelectSQL(entity, condition);
        Logger.logSQL("select SQL: ", sql);
        return getBeeSql().moreTableSelect(sql, entity);
    }
}